//
//  WelcomeViewController.swift
//  MyWeiboApp
//
//  Created by 胡海波 on 15/6/30.
//  Copyright © 2015年 胡海波. All rights reserved.
//

import UIKit
import SDWebImage
import FFAutoLayout

class WelcomeViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        // 设置头像
        if let urlString = sharedUserAccount?.avatar_large {
            iconView.sd_setImageWithURL(NSURL(string: urlString))
        }

        
    }

    override func viewDidAppear(animated: Bool) {

        super.viewDidAppear(animated)

        bottomConstraint?.constant = -(UIScreen.mainScreen().bounds.height - 200)

        UIView.animateWithDuration(1.0, delay: 0.2, usingSpringWithDamping: 0.8, initialSpringVelocity: 10.0, options: UIViewAnimationOptions.CurveLinear, animations: {

            self.view.layoutIfNeeded()

            }) { (_) -> Void in

                UIView.animateWithDuration(1.0, animations: {

                    self.messageLble.alpha = 1.0
                    }, completion: { _ in


                        NSNotificationCenter.defaultCenter().postNotificationName(SwitchRootVCNotification, object: true)
                        print("ok")


                })
        }
    }
    /// 定义头像的底部约束
    var bottomConstraint: NSLayoutConstraint?

    //MARK: - 设置界面控件
    override func loadView() {

        view = UIView()

        let backImageView = UIImageView(image: UIImage(named: "ad_background"))

        view.addSubview(backImageView)
        view.addSubview(iconView)
        view.addSubview(messageLble)

        // 设置自动布局
        // 1.背景图片
        backImageView.ff_Fill(view)

        // 2.图像
        let cons = iconView.ff_AlignInner(ff_AlignType.BottomCenter, referView: view, size: CGSizeMake(90, 90), offset: CGPoint(x: 0, y: -160))

        bottomConstraint = iconView.ff_Constraint(cons, attribute: NSLayoutAttribute.Bottom)

        // 欢迎信息
        messageLble.ff_AlignVertical(ff_AlignType.BottomCenter, referView: iconView, size: nil, offset: CGPoint(x: 0, y: 8))

    }

    // 创建图像
    lazy var iconView: UIImageView = {

        let iv = UIImageView(image: UIImage(named: "avatar_default_big"))
        iv.layer.masksToBounds = true
        iv.layer.cornerRadius = 45

        return iv
    }()

    // 欢迎信息
    lazy var messageLble: UILabel = {

        let lable = UILabel()
        lable.text = "欢迎归来"
        lable.sizeToFit()
        lable.textColor = UIColor.darkGrayColor()

        lable.alpha = 0

        return lable
    }()
}
